package package1;

import java.util.HashMap;
import java.util.Map;

public class TestForAlgorithm {

	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		
		jiaogucaixiang();
		
//		int[] arr = {20,10,3,6,7,8,9};
//		binarySearch(arr,6);
//		sortMethod(arr,true);
//		show(arr);
	}

	private static void jiaogucaixiang() {
		int times;
		Map<Integer,Integer>map = new HashMap<Integer,Integer>();
		for(int num=3;num<100;num++){
			int n = num;
			times = 0;
			while (n != 1) {
				if (n % 2 == 1)
					n = 3 * n + 1;
				else
					n /= 2;
//				System.err.println("times:"
//						+ (String.valueOf(times).length() == 1 ? ("0" + String.valueOf(times)) : String.valueOf(times))
//						+ "\tnumber:" + n);
				times++;
			}
			map.put(num, times);
		}	
		print(map);
	}


	private static void print(Map map) {
		for(Object key:map.keySet()){
			System.err.println(key+":"+map.get(key));
		}
		
	}

	private static void show(Object arr) {
		if(arr instanceof int[]) for(int a:(int[])arr) System.err.println(a);
	}

	private static void sortMethod(int[] arr, boolean order) {
		for(int i=0;i<arr.length;i++) for(int k=arr.length-1;k>i;k--)if(order?(arr[k]<arr[k-1]):(arr[k]>arr[k-1]))swap(arr,k,k-1);
	}

	private static void swap(int[] arr, int k, int i) {
		int t = arr[k];
		arr[k] = arr[i];
		arr[i] = t;
	}

	/**
	 * returns the position of the aim number [0, n]
	 * @param arr
	 * @param aim
	 */
	private static void binarySearch(int[] arr, int aim) {
		sortMethod(arr, true);
		show(arr);
		int max,min,mid;
		max = arr.length-1;
		min = 0;
		mid = (max+min)>>1;
		while(arr[mid]!=aim){
			if(arr[mid]>aim){
				max = mid-1;
				mid = (max+min)>>1;
			}else if(arr[mid]<aim){
				min = mid+1;
				mid = (max+min)>>1;
			}
		}
		System.err.println(aim+" position is ["+mid+"], starting from [0]");		
	}
}
